package com.xoa.service.equipment.impl;

import com.xoa.dao.equipment.EquipCapyMapper;
import com.xoa.dao.equipment.EquipTypeMapper;
import com.xoa.dao.equipment.EquipmentCapysMapper;
import com.xoa.dao.equipment.LimsEquipCapyTypeMapper;
import com.xoa.model.equipment.DataTree;
import com.xoa.model.equipment.EquipCapy;
import com.xoa.model.equipment.EquipType;
import com.xoa.model.equipment.LimsEquipCapyType;
import com.xoa.service.equipment.EquipCapyService;
import com.xoa.util.LimsJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class EquipCapyServiceImpl implements EquipCapyService {

    @Autowired
    private EquipCapyMapper equipCapyMapper;

    @Autowired
    private EquipmentCapysMapper equipmentCapysMapper;

    @Autowired
    private EquipTypeMapper equipTypeMapper;

    //@Autowired
    //private LimsCommonUnitsMapper limsCommonUnitsMapper;

    @Autowired
    private LimsEquipCapyTypeMapper limsEquipCapyTypeMapper;


    @Override
    public LimsJson<EquipCapy> selectEquipCapy(HttpServletRequest request, Integer page, Integer pageSize, Boolean useFlag, String typeCode) {
       LimsJson limsJson = new LimsJson(1,"err");

 /*       Map map = new HashMap();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(Integer.valueOf(request.getParameter("page")));
        pageParams.setPageSize(Integer.valueOf(request.getParameter("limit")));
//        map.put("page",pageParams);
        map.put("typeCode",typeCode);
       List<EquipCapy> equipCapies = equipCapyMapper.selectAllEquipCapy(map);
       List list = new ArrayList();

       if (equipCapies.size()>0){
           for (EquipCapy equipCapy:equipCapies){
               String names="";
               if(equipCapy.getUnitIds()!=null&&!equipCapy.getUnitIds().equals("")){
                   String[] id = equipCapy.getUnitIds().split(",");
                   for(int i=0;i<id.length;i++){
                       LimsCommonUnits limsCommonUnits = limsCommonUnitsMapper.selectByPrimaryKey(Integer.parseInt(id[i]));
                       if(limsCommonUnits!=null&&!limsCommonUnits.getUnitName().equals("")&&limsCommonUnits!=null){
                           names+=limsCommonUnits.getUnitName()+",";
                       }
                   }
               }
               equipCapy.setUnitName(names);
               list.add(equipCapy);
           }
//           int cont = equipCapyMapper.countAllEquipment();
           limsJson.setCode(0);
//           limsJson.setCount(cont);
//           limsJson.setMsg("成功查询到"+cont+"条数据");
           limsJson.setData(list);
           limsJson.setMsg("查询成功");
       }else {
           limsJson.setMsg("暂无数据");
       }*/
       return limsJson;
    }

    @Override
    public LimsJson addEquipCapy(List<EquipCapy> list) {
        LimsJson limsJson = new LimsJson(1,"err");

        try {
            int res=0;
            if (list!=null&&list.size()>0){
                for (EquipCapy equipCapy : list){
                    int i = equipCapyMapper.insertSelective(equipCapy);
                    if (i>0){
                        res++;
                    }
                }
                if(res>0){
                    limsJson.setCode(0);
                    limsJson.setCount(res);
                    limsJson.setMsg("成功插入"+res+"条数据");
                }else {
                    limsJson.setMsg("插入失败");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    @Override
    public LimsJson<EquipCapy> deleteEquipCapy(Integer capyId) {
        LimsJson limsJson = new LimsJson(1,"err");

        try {
            int cont =equipmentCapysMapper.countEquipmentBcapyId(capyId);
            if(cont>0){
                limsJson.setMsg("删除失败,此能力项下有关联设备，不能删除该能力项！");
            }else{
                int i = equipCapyMapper.deleteByPrimaryKey(capyId);
                if(i>0){
                    limsJson.setCode(0);
                    limsJson.setCount(i);
                    limsJson.setMsg("成功删除"+i+"条数据");
                    limsJson.setObject(capyId);
                }else {
                    limsJson.setMsg("删除失败");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    @Override
    public LimsJson<EquipCapy> deleteArrayEquipCapy(String ids) {
        LimsJson limsJson = new LimsJson(1,"err");
        try {
            String[] idArray = ids.split(",");
             for (int i=0;i<idArray.length;i++){
                 int cont =equipmentCapysMapper.countEquipmentBcapyId(Integer.parseInt(idArray[i]));
                 if(cont>0){
                     limsJson.setMsg("删除失败,选中的能力项下有关联设备，不能删除选中的能力项！");
                     continue;//忽略Integer.parseInt(idArray[i])时的循环
                 }else{
                     int mes = equipCapyMapper.deleteByPrimaryKey(Integer.parseInt(idArray[i]));
                     if(mes>0){//删除成功
                         limsJson.setCode(0);
                         limsJson.setMsg("删除成功");
                     }else {//失败
                         limsJson.setCode(1);
                         limsJson.setMsg("删除失败");
                     }
                 }

            }
        }catch (Exception e){//异常
            e.printStackTrace();
        }
        return limsJson;
    }

    @Override
    public LimsJson<EquipCapy> changeStatus(String capyIds,Integer status) {
        LimsJson limsJson = new LimsJson(1,"err");
        try {
            if (capyIds!=null){
                String[] capyId = capyIds.split(",");
                for (int i= 0;i<capyId.length;i++){
                    EquipCapy equipCapy = equipCapyMapper.selectByPrimaryKey(Integer.parseInt(capyId[i]));
                    if (equipCapy!=null){
                        equipCapy.setCapyStatus(status);
                        int mes = equipCapyMapper.updateByPrimaryKeySelective(equipCapy);
                        if(mes>0){
                            limsJson.setCode(0);
                            limsJson.setMsg("状态修改成功");
                            limsJson.setObject(equipCapy);
                        }else{
                            limsJson.setMsg("状态修改失败");
                        }
                    }
                }
            }else{
                limsJson.setMsg("请选择数据");
            }
        }catch (Exception e){//异常
            e.printStackTrace();
        }
        return limsJson;
    }

    public LimsJson<EquipCapy> editEquipCapy(EquipCapy equipCapy){
        LimsJson limsJson = new LimsJson(1,"err");
        try {
            if (equipCapy.getCapyId()!=null){
                int i = equipCapyMapper.updateByPrimaryKeySelective(equipCapy);
                if (i>0){
                    limsJson.setCode(0);
                    limsJson.setMsg("修改成功");
                    limsJson.setObject(equipCapy);
                }else {
                    limsJson.setMsg("修改失败");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
    * 创建作者:   刘欢
    * 创建日期:   16:54 2019/9/19
    * 方法介绍:   commonCode公共查询
    * 参数说明:   * @param parentTypeNo
    * @param codeTypeNo
    * @param taskId
    * @return     java.util.List<java.lang.Object>
    */
    public List<Object> selectCommonCode(String parentTypeNo,String codeTypeNo,Integer taskId){
        List list = new ArrayList();

        if(parentTypeNo!=null && parentTypeNo!=""){
            Map map = new HashMap();
            String[] codeNo = parentTypeNo.split(",");
            for(int i=0;i<codeNo.length;i++){
                map.put("parentTypeNo",codeNo[i]);
                if(taskId!=null){//在操作细则模块需要查询某个task下的状态，所以在这里加入taskId
                    map.put("taskId",taskId);
                }
                List<EquipCapy> newCommonCode = equipCapyMapper.selectCommonCode(map);
                list.add(newCommonCode);
            }
        }else if(codeTypeNo!=null&& !codeTypeNo.equals("")){
            String[] codeNo = codeTypeNo.split(",");
            for (int i=0;i<codeNo.length;i++){
                EquipCapy newCommonCode = equipCapyMapper.selectCommonCodeByTypeNo(codeNo[i],null);
                list.add(newCommonCode);
            }
        }
        return list;
    }

    public List<Map<String,Object>> selectEquipCapyEdit(Integer equipTypeId){

        //查询所有的能力项
        List<EquipCapy> list = equipCapyMapper.selectEquipCapy();
        List newList = new ArrayList();
        //查询选中类型下的设备能力项
        EquipType equipType =equipTypeMapper.selectByPrimaryKey(equipTypeId);
        try {
            if(list.size()>0&&!list.isEmpty()){
                if (equipType.getCapyIds()!=null&&!equipType.getCapyIds().equals("")){
                    //处理：假若该类型下有能力项，需要回显数据，
                    String[] id = equipType.getCapyIds().split(",");
                    if(id!=null&&!id.equals("")){
                        for (int i=0;i<id.length;i++){
                            EquipCapy equipCapy = equipCapyMapper.selectByPrimaryKey(Integer.parseInt(id[i]));
                            Map map = new HashMap();
                            map.put("id",equipCapy.getCapyId());
                            map.put("name", equipCapy.getCapyName());
                            map.put("typeCode",equipCapy.getTypeCode());
                            map.put("state", 1);
                            newList.add(map);
                        }
                    }
                    //处理所有能力项
                    for (EquipCapy equipCapy : list){
                        if(Arrays.asList(id).contains(equipCapy.getCapyId().toString())==true){
                            //这个退出表示如果结果集中的能力id在设备类型中的ids下，退出本次循环，
                            continue;
                        }else{
                            //如果结果集中的能力id不再设备类型中的ids下，返回数据
                            Map map = new HashMap();
                            map.put("id",equipCapy.getCapyId());
                            map.put("name",equipCapy.getCapyName());
                            map.put("typeCode",equipCapy.getTypeCode());
                            map.put("state", 0);
                            newList.add(map);
                        }
                    }

                }else{
                    for (EquipCapy equipCapy : list){
                            Map map = new HashMap();
                            map.put("id",equipCapy.getCapyId());
                            map.put("name",equipCapy.getCapyName());
                        map.put("typeCode",equipCapy.getTypeCode());

                        map.put("state", 0);
                            newList.add(map);

                    }
                }
                return newList;
            }else{
                //没有数据时
                Map map = new HashMap();
                map.put("result",false);
                map.put("mes","查询未果");
                newList.add(map);
                return  newList;
            }
        }catch ( Exception e){
            //出现异常时
            Map map = new HashMap();
            map.put("result",false);
            map.put("mes","查询异常");
            newList.add(map);
            return  newList;
        }
    }

    @Override
    public LimsJson equipTypeCodeList() {
        LimsJson limsJson = new LimsJson();
        List<LimsEquipCapyType> equipCapies = limsEquipCapyTypeMapper.equipTypeCodeList();
        if(equipCapies.size()>0){
            limsJson.setCode(0);
            limsJson.setData(equipCapies);
        }else{
            limsJson.setMsg("暂无数据");
        }
        return limsJson;
    }

    @Override
    public  LimsJson findBytyoeCodeTree(String typeCode, Integer equipTypeId) {
        LimsJson limsJson = new LimsJson();
        List firstList = new ArrayList();
        List<LimsEquipCapyType> equipCapies = limsEquipCapyTypeMapper.equipTypeCodeList();
        //查询选中类型下的设备能力项
        EquipType equipType =equipTypeMapper.selectByPrimaryKey(equipTypeId);
        if (equipCapies.size() > 0) {
            int FID=0;
            int m=1;
            int CID=0;
                int n=1;
            int sort1=0;
            int sort2=0;
            for (LimsEquipCapyType lect : equipCapies) {
                DataTree firsttree = new DataTree();
                FID= Integer.parseInt(lect.getTypeId()+"001");
                lect.setTypeId(FID);
                sort1= Integer.parseInt("0"+m);
                firsttree.setSort(sort1);
                m++;
                firsttree.setId(lect.getTypeId());
                firsttree.setLabel(lect.getTypeName());
                Map map = new HashMap();
                map.put("typeCode", lect.getTypeCode());
                List<EquipCapy> list = equipCapyMapper.findEquipCapybytypeCode(map);
                if (list.size() > 0) {

                    //有子级
                    firsttree.setIsLeaf(false);  //有子级
                    List secondList = new ArrayList();
                    int cont=0;
                    for (EquipCapy ec : list) {
                        DataTree secondTree = new DataTree();//循环第二层
                        secondTree.setStatus("0");
                        if (equipType.getCapyIds()!=null&&!equipType.getCapyIds().equals("")) {
                            String[] id = equipType.getCapyIds().split(",");
                            //处理：假若该类型下有能力项，需要回显数据，
                            if (id != null && !id.equals("")) {
                                for (int i = 0; i < id.length; i++) {
                                    if (id[i].equals(String.valueOf(ec.getCapyId()))) {
                                        secondTree.setStatus("1");
                                        cont++;
                                    }
                                }
                            }
                        }else{

                        }
                        CID= Integer.parseInt(ec.getCapyId()+"002");
                        ec.setCapyId(CID);
                        sort2= Integer.parseInt(n+"0");
                        secondTree.setSort(sort2);
                        n++;
                        secondTree.setId(ec.getCapyId());
                        secondTree.setLabel(ec.getCapyName());
                        secondTree.setIsLeaf(true);
                        secondList.add(secondTree);
                    }
                    if(list.size()==cont){
                        firsttree.setStatus("1");
                    } else if(list.size()>cont&&cont!=0){
                        firsttree.setStatus("2");
                    }else if(cont==0){
                        firsttree.setStatus("0");
                    }
                    firsttree.setChildren(secondList);

                }else{
                    firsttree.setIsLeaf(true);
                    firsttree.setStatus("0");

                }
                firstList.add(firsttree);
            }

        }
        limsJson.setCode(0);
        limsJson.setData(firstList);
        limsJson.setMsg("查询成功");
        return limsJson;

    }
}
